package com.deep.bilibili.controller;


import com.deep.bilibili.constant.CommonConstant;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.servlet.KaptchaExtend;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;


@Api(tags = "登录")
@Controller
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private Producer captchaProducer;

    @ApiOperation(value = "登录页面")
    @GetMapping("/admin/login")
    public String login(Model model) {
        logger.info(" login page = {}", model);
        return "login";
    }


    /**
     * 验证码
     */
    @RequestMapping("/admin/captcha")
    @ResponseBody
    public  void captcha(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        KaptchaExtend kaptchaExtend =  new KaptchaExtend();

        kaptchaExtend.captcha(request, response);

        String code = kaptchaExtend.getGeneratedKey(request);
        logger.info(" login captcha code = {}", code);
    }

    @ApiOperation(value = "验证码")
    @RequestMapping("/admin/kaptcha2")
    public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        // 生成验证码
        String capText = captchaProducer.createText();
        logger.info("kaptcha2 capText = {}", capText);
        session.setAttribute(CommonConstant.KAPTCHA_SESSION_KEY, capText);
        //向客户端写出
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }

    @GetMapping("/admin/login-error")
    public String loginError(HttpServletRequest request, Model model) {
        logger.info("login- error");
        model.addAttribute("loginError", "用户名或密码错误");
        request.getSession().setAttribute("loginError", "用户名或密码错误");
        return "login";
    }

    /**
     * @deprecated
     * @param model
     * @return
     */
    @GetMapping("/admin/login-captcha")
    public String loginCaptcha(Model model) {
        logger.info("login- error");
        model.addAttribute("loginError", "验证码错误");
        return "login";
    }
}
